package com.example.demo.service.impl;


import com.example.demo.entity.TPermission;
import com.example.demo.entity.TRole;
import com.example.demo.entity.TUser;
import com.example.demo.mapper.TPermissionMapper;
import com.example.demo.mapper.TRoleMapper;
import com.example.demo.mapper.TUserMapper;
import com.example.demo.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 何景航
 * @date 2025/3/4 15:25
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private TUserMapper tUserMapper;
    @Resource
    private TRoleMapper tRoleMapper;
    @Resource
    private TPermissionMapper tPermissionMapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        TUser tUser = tUserMapper.selectByLoginAct(username);
        if (tUser == null){
            throw new UsernameNotFoundException("登陆账号不存在");
        }
//        List<TRole> tRoles = tRoleMapper.selectByUserId(tUser.getId());
//        tUser.setRoles(tRoles);
        List<TPermission> tPermissionList = tPermissionMapper.selectByUserId(tUser.getId());
        tUser.setTPermissionList(tPermissionList);
        return tUser;
    }
}
